package com.niit.xlztest.controller;

import com.niit.xlztest.bean.Book;
import com.niit.xlztest.bean.BorrowRecord;
import com.niit.xlztest.bean.Comm;
import com.niit.xlztest.repository.BorrowRepository;
import com.niit.xlztest.repository.DetailRepository;
import com.niit.xlztest.service.BorrowService;
import com.niit.xlztest.service.CommService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.Base64;
import java.util.List;

@Controller
public class DetailController {
    @Autowired
    private DetailRepository detailRepository;
    @Autowired
    private BorrowRepository borrowRepository;
    @Autowired
    private BorrowService borrowService;
    @Autowired
    private CommService commService;

    @GetMapping("/findBookid")
    public String findBookBid(@RequestParam(name = "book_id") String book_id, Model model,HttpServletRequest request) {

        HttpSession session = request.getSession();
        session.setAttribute("user_id", 123);
        Book book = detailRepository.findBookBid(book_id);
        byte[] bookCover = book.getBook_cover();
        if (bookCover != null) { // 添加对 bookCover 是否为空的检测
            String bookCoverJpg = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bookCover);
            book.setBookCoverJpg(bookCoverJpg);
        }
        List<Comm> comms = commService.findCommByid(book_id);
        model.addAttribute("comms", comms);
        model.addAttribute("book", book);
        return "detail";
    }
    @GetMapping("/findBookidAdmin")
    public String findBookBidAdmin(@RequestParam(name = "book_id") String book_id, Model model,HttpServletRequest request) {

        HttpSession session = request.getSession();
        session.setAttribute("user_id", 123);

        Book book = detailRepository.findBookBid(book_id);
        byte[] bookCover = book.getBook_cover();
        if (bookCover != null) { // 添加对 bookCover 是否为空的检测
            String bookCoverJpg = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bookCover);
            book.setBookCoverJpg(bookCoverJpg);
        }
        List<Comm> comms = commService.findCommByid(book_id);
        model.addAttribute("comms", comms);
        model.addAttribute("book", book);
        return "detailAdmin";
    }

    @GetMapping("/goTo")
    public String goToAddUser() {
        return "Detailold";
    }


    @PostMapping ("/borrowBook")
    @Transactional
     public String borrowBook(@RequestParam(name = "book_id") String book_id, Model model,HttpServletRequest request ) {
        detailRepository.updateBook(book_id);

        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        BorrowRecord  borrowRecord = new BorrowRecord();
        borrowRecord.setBorrow_date(LocalDateTime.now());
        borrowRecord.setUser_id(user_id);
        int intbook_id = Integer.parseInt(book_id);
        borrowRecord.setBook_id(intbook_id);
        Book book = detailRepository.findBookBid(book_id);

        String book_name= book.getBook_name();
        borrowRecord.setBook_name(book_name);
        borrowService.borrow(borrowRecord);

        model.addAttribute("book", book);
        return "detail";
       }

    @PostMapping ("/BorrowRecord")
    @Transactional
    public String BorrowRecord(@ModelAttribute("BorrowRecord") BorrowRecord borrowRecord, Model model) {
        borrowRepository.save(borrowRecord);
        model.addAttribute("book");
        return "detail";
    }

    @GetMapping("/borrowPage")
    public String findUserId( Model model, HttpServletRequest request){

        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow";
    }
    @GetMapping("/borrow")
    public String findUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user_id", 123);
        return "redirect:/borrowPage";
    }

    @PostMapping("/deleteBorrowRecord")
    public String deleteBorrowRecord(@RequestParam("borrow_id") int borrow_id,Model model,HttpServletRequest request) {
        borrowRepository.deleteBorrowRecord(borrow_id);
        HttpSession session = request.getSession();

        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow";
    }

@PostMapping ("/replyBorrowRecord")
@Transactional
    public String updateBorrowDateByBookId(@RequestParam(name = "borrow_id") Integer borrow_id, Model model,HttpServletRequest request ){
        borrowRepository.updateBorrowDateByBookId(borrow_id);
        HttpSession session = request.getSession();
        Integer user_id = (Integer) session.getAttribute("user_id");

        List<BorrowRecord> findBorrowRecord = borrowRepository.findUserId(user_id);
        model.addAttribute("findBorrowRecord",findBorrowRecord);
        return "borrow" ;
    }




}



